<?php
namespace app\api\controller;

/*
 * 首页API
 */

class HomeController extends BaseController {

	/**
	 * 首页数据
	 */
	public function index() {
		$data['product'] = db('product')->field('id,name,thumb')->where('auditing', 1)->limit(3)->order('add_time desc')->select();
		$data['news'] = db('news a')->field('id,type,title,clicks,add_time')->limit(5)->order('clicks desc')->select();
		$data['video'] = db('vedio')->field('id,title,thumb')->limit(3)->order('add_time desc')->select();
		$data['expert'] = db('expert')->field('id,name,thumb,work_year,professional')->limit(2)->order('clicks desc')->select();
		$data['ask'] = db('ask a')->join('member m', 'm.id=a.uid')->field('m.thumb as head,m.nick_name,a.*')->limit(3)->order('add_time desc')->select();
		return $this->toData($data);
	}

	/**
	 * 产品列表
	 */
	public function product($cate_id1 = '', $cate_id2 = '', $qid = '', $sort = '', $page = 1) {
		$where['auditing'] = 1;
		if ($cate_id1) {
			$where['cate_id1'] = $cate_id1;
		}
		if ($cate_id2) {
			$where['cate_id2'] = $cate_id2;
		}
		if ($qid) {
			$where['qid'] = $qid;
		}
		if ($sort) {
			$sort .= ' desc';
		}
		$list = db('product')->where($where)->order($sort)->page($page, 10)->select();
		if ($list) {
			return $this->toData($list);
		} else {
			return $this->toError('没有数据');
		}
	}

	/**
	 * 人才列表
	 */
	public function people($cate_id = '', $work_type = '', $qid = '', $page = 1) {
		$where['auditing'] = 1;
		if ($cate_id) {
			$where['cate_id1'] = $cate_id1;
		}
		if ($work_type) {
			$where['work_type'] = $work_type;
		}
		if ($qid) {
			$where['qid'] = $qid;
		}
		$list = db('people')->where($where)->order('add_time desc')->page($page, 10)->select();
		if ($list) {
			return $this->toData($list);
		} else {
			return $this->toError('没有数据');
		}
	}

	/**
	 * 农机列表
	 */
	public function machine($cate_id = '', $qid = '', $sort = '', $page = 1) {
		$where['auditing'] = 1;
		if ($cate_id) {
			$where['cate_id'] = $cate_id;
		}
		if ($qid) {
			$where['qid'] = $qid;
		}
		if ($sort) {
			$sort .= ' desc';
		}
		$list = db('machine')->where($where)->order($sort)->page($page, 10)->select();
		if ($list) {
			return $this->toData($list);
		} else {
			return $this->toError('没有数据');
		}
	}

	/**
	 * 农家乐列表
	 */
	public function farmstay($cate_id = '', $qid = '', $sort = '', $page = 1) {
		$where['auditing'] = 1;
		if ($cate_id) {
			$where['cate_id'] = $cate_id;
		}
		if ($qid) {
			$where['qid'] = $qid;
		}
		if ($sort) {
			$sort .= ' desc';
		}
		$list = db('farmstay')->where($where)->order($sort)->page($page, 10)->select();
		if ($list) {
			return $this->toData($list);
		} else {
			return $this->toError('没有数据');
		}
	}

}
